package com.telkomsigma.spring.hibernate.controller;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.telkomsigma.spring.hibernate.persistence.domain.FootballTeam;
import com.telkomsigma.spring.hibernate.service.TeamService;
import com.telkomsigma.spring.hibernate.service.TeamServiceImpl;

@Controller
@SessionAttributes
public class TeamController {
	
	ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
	TeamService teamDao = (TeamService) context.getBean("serviceTeam");
	//PlayerService playerDao = (PlayerService) context.getBean("playerService");

	FootballTeam team = new FootballTeam();
//	Player player = new Player();
	
	@RequestMapping(value = "/team", method = RequestMethod.GET)
	public ModelAndView showTeam() {
		//ambil data
		List<FootballTeam> teamList = teamDao.findAll();
		return new ModelAndView("showTeam", "teamList", teamList);
	}

	//buat manggil form input team
	@RequestMapping(value = "/addTeam", method = RequestMethod.GET)
	public ModelAndView addTeam() {
		return new ModelAndView("addTeam");
	}
	
	@RequestMapping(value = "/welcome", method = RequestMethod.GET)
	public ModelAndView welcome() {
		return new ModelAndView("welcome");
	}
	
	@RequestMapping(value = "/insertTeam", method = RequestMethod.POST)
	public ModelAndView insertTeam(@RequestParam("name") String teamName) {
	//	team.setIdTeam(teamId);
		team.setName(teamName);
		teamDao.insert(team);
		
		List<FootballTeam> teamList = teamDao.findAll();
		return new ModelAndView("showTeam", "teamList", teamList);
	}
	
	@RequestMapping(value = "/deleteTeam", method = RequestMethod.GET)
	public ModelAndView deleteTeam(@RequestParam("id") int teamId) {
		team.setId(teamId);
		teamDao.remove(team);
		
		List<FootballTeam> teamList = teamDao.findAll();
		return new ModelAndView("showTeam", "teamList", teamList);
	}
	
	@RequestMapping(value = "/updateTeam", method = RequestMethod.GET)
	public ModelAndView updateTeam(@RequestParam("id") int teamId) {
		//team.setIdTeam(teamId);
		team = teamDao.findById(teamId);
		return new ModelAndView("updateTeam","team",team);
	}
	
	@RequestMapping(value = "/editTeam", method = RequestMethod.POST)
	public ModelAndView editTeam(@RequestParam("idTeam") int teamId, @RequestParam("teamName") String teamName) {
		FootballTeam team = new FootballTeam();
		team.setId(teamId);
		team.setName(teamName);
		
		teamDao.update(team);
		
		List<FootballTeam> teamList = teamDao.findAll();
		return new ModelAndView("showTeam", "teamList", teamList);
	}
	
	

}


